# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library.

cmake_minimum_required(VERSION 3.18.1)
# Declares and names the project.
project("musicTools")

# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.
file(GLOB SRC_FFMPEG
        jniHandle.c
        #        ffmpeg_cmd.c
        json/cJSON.c
        ffmpeg/*.c)

set(GO_BUILD_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../../../libs)
set(SRC ${GO_BUILD_PATH}/${ANDROID_ABI})
file(GLOB LIBFILE
        ${SRC}/*.so
)
#set(SRC_VISUALIZER
#        visualizer/fft.cpp
#        visualizer/fixed_fft.cpp
#        visualizer/frank_visualizer.cpp
#        visualizer/frank_visualizer_jni.cpp
#        visualizer/window.cpp)
#
#set(SRC_METADATA
#        metadata/media_retriever.cpp
#        metadata/media_retriever_jni.cpp
#        metadata/metadata_util.c
#        metadata/ffmpeg_media_retriever.c)

# set(SRC_FFPLAYER ffplayer)
add_library( # Sets the name of the library.
             handle

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
            ${SRC_FFMPEG}
            # ${SRC_VISUALIZER}
            # ${SRC_METADATA}
            # ${SRC_FFPLAYER}
            )

add_library( ffmpeg
            SHARED
            IMPORTED )
set_target_properties( ffmpeg
                    PROPERTIES IMPORTED_LOCATION
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../libs/${CMAKE_ANDROID_ARCH_ABI}/libffmpeg.so )

add_library( um
            SHARED
            IMPORTED )
set_target_properties( um
                    PROPERTIES IMPORTED_LOCATION
        ${CMAKE_CURRENT_SOURCE_DIR}/../../../libs/${CMAKE_ANDROID_ARCH_ABI}/libum.so )

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")

include_directories(${ANDROID_NDK}/../23.1.7779620/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include)
#尝试过使用ndk/21.0.6113669 的include，但是编译出现问题，而项目ffmpeg貌似不支持新版本的ndk，所以使用老版本环境，但是使用新版本的include
include_directories(.)
include_directories(include)
#include_directories(metadata)
#include_directories(ffplayer)
#include_directories(/home/zhanghj/work/AndroidSdk/ndk/23.1.7779620/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include)

include_directories(include/${ANDROID_ABI}/libavcodec)
include_directories(include/${ANDROID_ABI}/libavutil)

find_library( # Sets the name of the path variable.
              log-lib
              log
        #       std-lib
        #       android #好像没用，留着备用
               )

target_link_libraries( # 编译需要链接的库
                        handle
                        -Wl,--start-group   #使链接库不受依赖顺序影响
                        ${LIBFILE}          #此处链接的第三方库必须指定位置，不然运行会报错找不到该库,最好使用file()获取
                        -Wl,--end-group
                        -landroid #native_window
                        -ljnigraphics #bitmap
                        -lOpenSLES #openSLES
                        # Links the target library to the log library
                        # included in the NDK.
                        ${log-lib} )
